﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace YBOT_Field_Control_2016
{
    public partial class Score : Form
    {

        public bool finalScore = false;
        public bool manualOverride = false;
        GameControl game;

        public Score()
        {
            InitializeComponent();
            this.game = new GameControl();
        }

        public Score(GameControl _game)
        {
            InitializeComponent();
            this.game = _game;
        }

        private void Score_Shown(object sender, EventArgs e)
        {
            mtbGreenAutoLights.Text = this.game.green.autoLightScore.ToString();
            mtbGreenAutoFoS.Text = this.game.green.autoFoSScore.ToString();
            mtbGreenManLights.Text = this.game.green.manualLightScore.ToString();
            mtbGreenManFoS.Text = this.game.green.manualFoSscore.ToString();
            mtbGreenPenalty.Text = this.game.green.penalty.ToString();

            mtbRedAutoLights.Text = this.game.red.autoLightScore.ToString();
            mtbRedAutoFoS.Text = this.game.red.autoFoSScore.ToString();
            mtbRedManLights.Text = this.game.red.manualLightScore.ToString();
            mtbRedManFoS.Text = this.game.red.manualFoSscore.ToString();
            mtbRedPenalty.Text = this.game.red.penalty.ToString();

            if (this.game.green.dq) button1.PerformClick();
            if (this.game.red.dq) button2.PerformClick();

            this.game.green.autoScore = (this.game.green.autoLightScore * 2) + (this.game.green.autoFoSScore);
            this.game.green.manScore = (this.game.green.manualLightScore * 2) + (this.game.green.manualFoSscore);
            this.game.green.ringScore = (this.game.green.corner) + (this.game.green.head * 4) + (this.game.green.fingers * 3) + (this.game.green.foot * 2);
            this.game.green.finalScore = this.game.green.autoScore + this.game.green.manScore + this.game.green.ringScore;

            this.game.red.autoScore = (this.game.red.autoLightScore * 2) + (this.game.red.autoFoSScore);
            this.game.red.manScore = (this.game.red.manualLightScore * 2) + (this.game.red.manualFoSscore);
            this.game.red.ringScore = (this.game.red.corner) + (this.game.red.head * 4) + (this.game.red.fingers * 3) + (this.game.red.foot * 2);
            this.game.red.finalScore = this.game.red.autoScore + this.game.red.manScore + this.game.red.ringScore;

            this.game.green.score = this.game.green.finalScore;
            this.game.red.score = this.game.red.finalScore;

            this.game.green.finalScore = this.game.green.score - (this.game.green.penalty * 10);
            this.game.red.finalScore = this.game.red.score - (this.game.red.penalty * 10);

            this.mtbGreenFinalScore.Text = this.game.green.finalScore.ToString();
            this.mtbRedFinalScore.Text = this.game.red.finalScore.ToString();

        }

        private void updateFinalScore()
        {
            this.game.green.autoLightScore = Convert.ToInt32(mtbGreenAutoLights.Text);
            this.game.green.autoFoSScore = Convert.ToInt32(mtbGreenAutoFoS.Text);
            this.game.green.manualLightScore = Convert.ToInt32(mtbGreenManLights.Text);
            this.game.green.manualFoSscore = Convert.ToInt32(mtbGreenManFoS.Text);
            this.game.green.corner = Convert.ToInt32(mtbGreenCornerRings.Text);
            this.game.green.head = Convert.ToInt32(mtbGreenHead.Text);
            this.game.green.fingers = Convert.ToInt32(mtbGreenFingers.Text);
            this.game.green.foot = Convert.ToInt32(mtbGreenFeet.Text);
            this.game.green.penalty = Convert.ToInt32(mtbGreenPenalty.Text);

            this.game.red.autoLightScore = Convert.ToInt32(mtbRedAutoLights.Text);
            this.game.red.autoFoSScore = Convert.ToInt32(mtbRedAutoFoS.Text);
            this.game.red.manualLightScore = Convert.ToInt32(mtbRedManLights.Text);
            this.game.red.manualFoSscore = Convert.ToInt32(mtbRedManFoS.Text);
            this.game.red.corner = Convert.ToInt32(mtbRedCornerRings.Text);
            this.game.red.head = Convert.ToInt32(mtbRedHead.Text);
            this.game.red.fingers = Convert.ToInt32(mtbRedFingers.Text);
            this.game.red.foot = Convert.ToInt32(mtbRedFeet.Text);
            this.game.red.penalty = Convert.ToInt32(mtbRedPenalty.Text);

            this.game.green.autoScore = (this.game.green.autoLightScore * 2) + (this.game.green.autoFoSScore);
            this.game.green.manScore = (this.game.green.manualLightScore * 2) + (this.game.green.manualFoSscore);
            this.game.green.ringScore = (this.game.green.corner) + (this.game.green.head * 4) + (this.game.green.fingers * 3) + (this.game.green.foot * 2);
            this.game.green.finalScore = this.game.green.autoScore + this.game.green.manScore + this.game.green.ringScore;

            this.game.red.autoScore = (this.game.red.autoLightScore * 2) + (this.game.red.autoFoSScore);
            this.game.red.manScore = (this.game.red.manualLightScore * 2) + (this.game.red.manualFoSscore);
            this.game.red.ringScore = (this.game.red.corner) + (this.game.red.head * 4) + (this.game.red.fingers * 3) + (this.game.red.foot * 2);
            this.game.red.finalScore = this.game.red.autoScore + this.game.red.manScore + this.game.red.ringScore;

            this.game.green.score = this.game.green.finalScore;
            this.game.red.score = this.game.red.finalScore;

            this.game.green.finalScore = this.game.green.score - (this.game.green.penalty * 10);
            this.game.red.finalScore = this.game.red.score - (this.game.red.penalty * 10);

            this.mtbGreenFinalScore.Text = this.game.green.finalScore.ToString();
            this.mtbRedFinalScore.Text = this.game.red.finalScore.ToString();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(button1.BackColor == DefaultBackColor)
            {
                button1.BackColor = Color.Lime;
                this.game.green.dq = true;
            }
            else
            {
                button1.BackColor = DefaultBackColor;
                this.game.green.dq = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.BackColor == DefaultBackColor)
            {
                button2.BackColor = Color.Red;
                this.game.red.dq = true;
            }
            else
            {
                button2.BackColor = DefaultBackColor;
                this.game.red.dq = false;
            }
        }

        private void btnFinalScore_Click(object sender, EventArgs e)
        {

            updateFinalScore();



            this.finalScore = true;

        }

        private void btnUpdateScore_Click(object sender, EventArgs e)
        {
            updateFinalScore();
        }


        private void MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            MessageBox.Show("Enter Valid Number", "ERROR");
        }

        private void Score_FormClosed(object sender, FormClosedEventArgs e)
        {
            updateFinalScore();
            this.finalScore = true;
        }
    }
}
